import requests
import json

def send_text_message(webhook_url, content):
    """发送文本消息到企业微信"""
    headers = {'Content-Type': 'application/json'}
    data = {
        "msgtype": "text",
        "text": {
            "content": content
        }
    }
    response = requests.post(webhook_url, headers=headers, data=json.dumps(data))
    return response.json()

def send_news_message(webhook_url, news_items):
    """发送图文消息到企业微信"""
    headers = {'Content-Type': 'application/json'}
    data = {
        "msgtype": "news",
        "news": {
            "articles": news_items
        }
    }
    response = requests.post(webhook_url, headers=headers, data=json.dumps(data))
    return response.json()

def send_message(task):
    """根据任务类型发送相应的消息"""
    if task.message_type == 'text':
        return send_text_message(task.webhook_url, task.content)
    elif task.message_type == 'news':
        # 图文消息的content应该是JSON格式的数组
        news_items = json.loads(task.content)
        return send_news_message(task.webhook_url, news_items)
    else:
        raise ValueError(f"不支持的消息类型: {task.message_type}")